package org.eclipse.rally.plugin.views;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.rally.plugin.utils.RallyConstants;
import org.eclipse.rally.plugin.utils.SWTResourceManager;
import org.eclipse.rally.plugin.webservice.RallyMain;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;

import com.rallydev.webservice.v1_12.domain.HierarchicalRequirement;
import com.rallydev.webservice.v1_12.domain.Task;


public class UserStoryTabGUI extends ApplicationWindow {
    public static Text text_4;
    public static Group grpUserStory;
    public static int minDefVal;
    public static int maxDefVal;
    public static int defPageNumber;
    public static Table table_2;
    public static List lstDefects;
    public static Text text_2;
    public static Tree tree;
    public static CCombo combo1;
    public static CCombo combo2;
    public static TreeItem item,subItem;
    public final static Composite parent = null;
    public static TabFolder tabFolder;
    public static  Map userStoryMap = new HashMap(); 
    final Display display = Display.getCurrent();
    final Cursor handCursor = new Cursor(display, SWT.CURSOR_HAND);
    public static TabItem tabItem_1;
    public UserStoryTabGUI() {
        super(null);
    }

    public static void main(String[] args) {
        //parent.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
        Display display = new Display();
        Shell shell = new Shell(display);

        TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
        tabFolder.setBounds(0, 0, 950, 800);

        //dashBoardGUI();
        Composite composite = new Composite(tabFolder, SWT.NONE);
  //    new UserStoryTabGUI().UserStoryTabData(tabFolder);

        shell.pack();
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
    public void backToUserStoriesPage(TabFolder tabFolder_1) {
        tabFolder = tabFolder_1;
        UserStoryTabData(tabFolder,false);
    }
    public  Group UserStoryTabData(TabFolder tabFolder_1, boolean objChk) {
    	
    	tabFolder=tabFolder_1;
    	if(objChk){
    	tabItem_1 = new TabItem(tabFolder, SWT.NONE);
        tabItem_1.setText("User &Stories");
    	}
        Composite composite = new Composite(tabFolder, SWT.BORDER);

        tabItem_1.setControl(composite);

        grpUserStory = new Group(composite, SWT.BORDER);
        grpUserStory.setBounds(2, 25, 850, 300);
        grpUserStory.setText("User Stories");
        grpUserStory.setBackground(SWTResourceManager.getColor(
                SWT.COLOR_LIST_BACKGROUND));

        {
            Label label3 = new Label(grpUserStory, SWT.CENTER);
            label3.setText("Views :");
            label3.setForeground(SWTResourceManager.getColor(
                    SWT.COLOR_WIDGET_DARK_SHADOW));
            label3.setBackground(SWTResourceManager.getColor(
                    SWT.COLOR_LIST_BACKGROUND));
            label3.setBounds(10, 23, 36, 13);
        }

        {
            CCombo comboj = new CCombo(grpUserStory, SWT.BORDER);
            comboj.setText("Quick Views");
            comboj.setItems(new String[] {
                    "All Test Cases", "Never Run", "Not Passing ",
                    "Active Defects"
                });
            comboj.setEditable(false);
            comboj.setBackground(SWTResourceManager
            		.getColor(SWT.COLOR_LIST_BACKGROUND));
            comboj.setBounds(52, 23, 105, 21);
        }

        {
            Label label4 = new Label(grpUserStory, SWT.CENTER);
            label4.setText("ID");
            label4.setForeground(SWTResourceManager.getColor(
                    SWT.COLOR_TITLE_BACKGROUND));
            label4.setBackground(SWTResourceManager.getColor(
                    SWT.COLOR_LIST_BACKGROUND));
            label4.setBounds(0, 60, 36, 13);
        }

        {
            Label label5 = new Label(grpUserStory, SWT.NONE);
            label5.setText("Type");
            label5.setForeground(SWTResourceManager.getColor(
                    SWT.COLOR_TITLE_BACKGROUND));
            label5.setBackground(SWTResourceManager.getColor(
                    SWT.COLOR_LIST_BACKGROUND));
            label5.setAlignment(SWT.CENTER);
            label5.setBounds(144, 60, 49, 13);
        }

        {
            Label label6 = new Label(grpUserStory, SWT.NONE);
            label6.setText("Last Verdict");
            label6.setForeground(SWTResourceManager.getColor(
                    SWT.COLOR_TITLE_BACKGROUND));
            label6.setBackground(SWTResourceManager.getColor(
                    SWT.COLOR_LIST_BACKGROUND));
            label6.setBounds(278, 60, 65, 13);
        }

        text_2 = new Text(grpUserStory, SWT.BORDER);
        text_2.setBounds(10, 79, 76, 19);

        {
            CCombo comboo = new CCombo(grpUserStory, SWT.BORDER);
            comboo.setText("All");
            comboo.setItems(new String[] {
                    "Acceptence", "Functional", "Performance", "Regression",
                    "Usability", "User Interface"
                });
            comboo.setEditable(false);
            comboo.setBackground(SWTResourceManager
            		.getColor(SWT.COLOR_LIST_BACKGROUND));
            comboo.setBounds(157, 79, 88, 21);
        }

        {
            CCombo combooo = new CCombo(grpUserStory, SWT.BORDER);
            combooo.setText("All");
            combooo.setItems(new String[] {
                    "Bocked", "Error", "Fail", "Inconclusive", "Pass"
                });
            combooo.setEditable(false);
            combooo.setBackground(SWTResourceManager
            		.getColor(SWT.COLOR_LIST_BACKGROUND));
            combooo.setBounds(278, 79, 88, 21);
        }

        {
            final Label clbl_Filter = new Label(grpUserStory, SWT.NONE);
            clbl_Filter.setImage(SWTResourceManager.getImage(
                    DashboardGUI.class, "/icons/Search-enabled.gif"));
            clbl_Filter.setBounds(395, 79, 58, 23);
            clbl_Filter.setBackground(SWTResourceManager
    				.getColor(SWT.COLOR_LIST_BACKGROUND));
            clbl_Filter.setCursor(handCursor);
        }

        {
            Label label7 = new Label(grpUserStory,
                    SWT.SEPARATOR | SWT.HORIZONTAL);
            label7.setForeground(SWTResourceManager.getColor(
                    SWT.COLOR_DARK_BLUE));
            label7.setBackground(SWTResourceManager.getColor(
                    SWT.COLOR_DARK_BLUE));
            label7.setAlignment(SWT.CENTER);
            label7.setBounds(10, 104, 446, 2);
        }
        tree = new Tree(grpUserStory, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL |SWT.MULTI | SWT.FULL_SELECTION);
    	tree.setHeaderVisible(true);
    	 tree.setLinesVisible(true);
    	tree.setBounds(11, 104, 820, 160);
    	TreeColumn column1 = new TreeColumn(tree, SWT.CENTER);
    	column1.setText("ID");
    	column1.setWidth(100);
    	TreeColumn column2 = new TreeColumn(tree, SWT.CENTER);
    	column2.setText("Name");
    	column2.setWidth(250);
    	TreeColumn column3 = new TreeColumn(tree, SWT.CENTER);
    	column3.setText("Type");
    	column3.setWidth(200);
    	TreeColumn column4 = new TreeColumn(tree, SWT.CENTER);
    	column4.setText("Owner");
    	column4.setWidth(250);
    	populateUserStories();
    	
    	tree.addListener (SWT.MouseDoubleClick, new Listener () {
    		public void handleEvent (Event event) {
    			//String string = event.detail == SWT.CHECK ? "Checked" : "Selected";
    			TreeItem tableItemStr = null;
    			TreeItem[] selection = tree.getSelection();
                tableItemStr = (TreeItem) selection[selection.length -
                    1];
                if (tableItemStr.getText().startsWith("TA"))
                		{
    		//	System.out.println (event.item + " " + tableItemStr.getText());
    			//JOptionPane.showMessageDialog(null,tableItemStr.getText(), "", 0);
    			 new TaskDetailGUI().taskDetail(tabFolder,tabItem_1,HeaderPage.taskglobalSearch(tableItemStr.getText()),RallyConstants.PAGE_USERSTORIES);
                		}
                else
                {
                	JOptionPane.showMessageDialog(null,tableItemStr.getText(), "", 0);
                	new UserStoryDetailGUI().userStoryDetail(tabFolder, tabItem_1, String.valueOf(userStoryMap.get(tableItemStr.getText())), RallyConstants.PAGE_USERSTORIES);
                }
    		}
		       
    	});
    	
        final CLabel clbl_First = new CLabel(grpUserStory, SWT.NONE);
        clbl_First.setBackground(SWTResourceManager.getColor(
                SWT.COLOR_LIST_BACKGROUND));
        clbl_First.setImage(SWTResourceManager.getImage(DefectsTabGUI.class,
                "/icons/Button-MoveAll.gif"));
        clbl_First.setBounds(470, 270, 50, 17);
        clbl_First.setCursor(handCursor);
        clbl_First.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseDown(MouseEvent e) {
                    if (minDefVal > 1) {
                        minDefVal -= maxDefVal;
                        defPageNumber--;
                        text_4.setText(defPageNumber + "");
                    }

                    //	populateTestCase();
                }
            });

        final CLabel clbl_Last = new CLabel(grpUserStory, SWT.NONE);
        clbl_Last.setBackground(SWTResourceManager.getColor(
                SWT.COLOR_LIST_BACKGROUND));
        clbl_Last.setImage(SWTResourceManager.getImage(DefectsTabGUI.class,
                "/icons/Button-RemoveAll.gif"));
        clbl_Last.setBounds(300, 270, 50, 17);
        clbl_Last.setCursor(handCursor);
        clbl_Last.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseDown(MouseEvent e) {
                    if (minDefVal > 1) {
                        minDefVal -= maxDefVal;
                        defPageNumber--;
                        text_4.setText(defPageNumber + "");
                    }

                    //populateTestCase();
                }
            });
        clbl_Last.setCursor(handCursor);
        final CLabel clbl_Next = new CLabel(grpUserStory, SWT.NONE);
        clbl_Next.setBackground(SWTResourceManager.getColor(
                SWT.COLOR_LIST_BACKGROUND));
        clbl_Next.setImage(SWTResourceManager.getImage(DefectsTabGUI.class,
                "/icons/FF-enabled.gif"));
        clbl_Next.setBounds(425, 270, 50, 17);
        clbl_Next.setCursor(handCursor);
        clbl_Next.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseDown(MouseEvent e) {
                    if (minDefVal > 1) {
                        minDefVal -= maxDefVal;
                        defPageNumber--;
                        text_4.setText(defPageNumber + "");
                    }

                    // populateDefects();

                    // populateDefects();
                }
            });

        final CLabel clbl_Previous = new CLabel(grpUserStory, SWT.NONE);
        clbl_Previous.setBackground(SWTResourceManager.getColor(
                SWT.COLOR_LIST_BACKGROUND));
        clbl_Previous.setImage(SWTResourceManager.getImage(
                DefectsTabGUI.class, "/icons/Button-REW.gif"));
        clbl_Previous.setBounds(360, 270, 50, 17);
        clbl_Previous.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseDown(MouseEvent e) {
                    if (minDefVal > 1) {
                        minDefVal -= maxDefVal;
                        defPageNumber--;
                        text_4.setText(defPageNumber + "");
                    }

                    // populateDefects();
                }
            });
        clbl_Previous.setCursor(handCursor);

        text_4 = new Text(grpUserStory, SWT.BORDER);
        text_4.setBounds(560, 270, 36, 19);
        text_4.setText(defPageNumber + "");

        Label lblPageNumber = new Label(grpUserStory, SWT.NONE);
        lblPageNumber.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
        lblPageNumber.setBounds(530, 270, 49, 13);
        lblPageNumber.setText("P No :");

        return grpUserStory;
    }
    private void populateUserStories(){

    	try{

    	HierarchicalRequirement hierarchicalRequirement = new HierarchicalRequirement();

    	RallyMain rallyMain = new RallyMain();

    	List list = rallyMain.searchObject(hierarchicalRequirement, "HierarchicalRequirement", 1, 10);

    	for (Iterator iterator = list.iterator(); iterator.hasNext();) {

    	HierarchicalRequirement requirement = (HierarchicalRequirement) iterator.next();
    	 item = new TreeItem(tree, SWT.NONE);
    	 
    	//System.out.println("requirement.getName()::: "+requirement.getName());
    	item.setText(new String[] { requirement.getFormattedID() ,requirement.getName(),requirement.getType(),requirement.getOwner() });
    	userStoryMap.put(requirement.getFormattedID(), requirement.getRef());
    	Task[] tasks = requirement.getTasks();

    	for (int i = 0; i < tasks.length; i++) {

    	Task task = (Task)RallyMain.readTaskObject(tasks[i].getRef());
    	 subItem = new TreeItem(item, SWT.NONE);
    	 subItem.setText(new String[] {task.getFormattedID() ,task.getName(), task.getType(),task.getOwner() });
    	
    	
    	}

    	}

    	}catch (Exception e) {

    	// TODO: handle exception

    	e.printStackTrace();

    	}

    	}
}


